import 'package:extended_text/extended_text.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class ExpandableText extends StatefulWidget {
  final String? text; //文本
  final Widget? btnOpen; //打开文本
  final double? btnOpenWidget; //打开按钮的宽度
  final Widget? btnClose; //收起文本
  final int? maxLines; //最大展示行数
  final TextStyle? style; //文本样式
  final bool? expand; //展开收起判断条件
  final TextAlign? textAlign; //文本定位
  final String? roteName; //跳转路由名称
  final Map<String, dynamic>? pageParam; //路由参数

  const ExpandableText(
      {Key? key,
      this.text,
      this.btnOpen,
      this.btnOpenWidget,
      this.btnClose,
      this.maxLines,
      this.style,
      this.expand,
      this.textAlign,
      this.roteName,
      this.pageParam})
      : super(key: key);

  @override
  _ExpandableTextState createState() => _ExpandableTextState(
      text,
      btnOpen,
      btnOpenWidget,
      btnClose,
      maxLines,
      style,
      expand,
      textAlign,
      roteName,
      pageParam);
}

class _ExpandableTextState extends State<ExpandableText> {
  final String? text;
  final Widget? btnOpen;
  final double? btnOpenWidget;
  final Widget? btnClose;
  final int? maxLines;
  final TextStyle? style;
  final TextAlign? textAlign;
  final String? roteName;
  final Map<String, dynamic>? pageParam;
  bool? expand;

  _ExpandableTextState(
      this.text,
      this.btnOpen,
      this.btnOpenWidget,
      this.btnClose,
      this.maxLines,
      this.style,
      this.expand,
      this.textAlign,
      this.roteName,
      this.pageParam) {
    if (expand == null) {
      expand = false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (context, size) {
      final span = TextSpan(text: widget.text ?? '', style: widget.style);
      final tp = TextPainter(
          text: span, maxLines: maxLines, textDirection: TextDirection.ltr);
      tp.layout(maxWidth: size.maxWidth);
      if (tp.didExceedMaxLines) {
        return expand!
            ? GestureDetector(
          onTap: (){
            setState(() {
              expand = !expand!;
            });
          },
          child: RichText(
            text: TextSpan(children: <InlineSpan>[
              TextSpan(text: widget.text, style: widget.style),
              WidgetSpan(
                child: GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    setState(() {
                      expand = !expand!;
                    });
                  },
                  child: Container(child: btnClose),
                ),
              ),
            ]),
          ),
        )
            : GestureDetector(
          onTap: (){
            setState(() {
              if (widget.roteName == null) {
                expand = !expand!;
              } else {
                Navigator.pushNamed(context, widget.roteName!,
                    arguments: widget.pageParam);
              }
            });
          },
          child: ExtendedText(
            widget.text!,
            style: widget.style,
            maxLines: maxLines,
            textAlign: textAlign,
            overflow: TextOverflow.ellipsis,
            overflowWidget: TextOverflowWidget(
              align: TextOverflowAlign.left,
              child: Stack(
                clipBehavior: Clip.none,
                children: [
                  GestureDetector(
                    child: RichText(
                        textAlign: TextAlign.left,
                        text: TextSpan(
                            text: '...',
                            style: widget.style,
                            children: <InlineSpan>[
                              WidgetSpan(child: btnOpen!),
                            ])),
                    onTap: () {
                      setState(() {
                        if (widget.roteName == null) {
                          expand = !expand!;
                        } else {
                          Navigator.pushNamed(context, widget.roteName!,
                              arguments: widget.pageParam);
                        }
                      });
                    },
                  ),
                  Container(
                    width: btnOpenWidget ?? 45,
                  ),
                  // Positioned(
                  //     top: -2,
                  //     left: 2,
                  //     child:
                  // ),
                ],
              ),
            ),
          ),
        );
      } else {
        print(tp.didExceedMaxLines);
        return Text(widget.text ?? '', style: widget.style);
      }
    });
  }
}
